﻿using System;
using System.Web.UI;
using MS.InformationManagement.Presenter.DocumentPrinter;
using MS.InformationManagement.Presenter.Support;
using System.Web.UI.WebControls;
using MS.InformationManagement.DAL.DocumentCenter;
using System.Collections.Generic;
using System.Text;
using MS.InformationManagement.Support;
using MS.InformationManagement.Presenter.ObserverImplementation;
using MS.InformationManagement.DAL.DTOs;

namespace MS.InformationManagement.Controls._CONTROLTEMPLATES.InformationManagerControls
{
    public partial class PrintableDocumentList : BaseControl, IPrintableDocumentListView, IBaseObserver
    {

        #region [Private Fields]

        private PrintableDocumentListPresenter _Presenter;

        #endregion

        #region Public Methods

        public bool UpdateRecordStatus { get; set; }

        public string SelectedContentType { get; set; }

        public IBaseSubject ParentSubject { get; set; }

        public string SelectedDocumentPath { get; set; }

        public int SelectedItemId { get; set; }

        public string Operation { get; set; }

        public List<DocumentToPrint> Documents { get; set; }

        public int PageIndex
        {
            get { return Pager1.PageIndex; }
            set { Pager1.PageIndex = value; }
        }

        public int PageSize
        {
            get { return Pager1.PageSize; }
            set { Pager1.PageSize = value; }
        }

        public int TotalRecords
        {
            get { return Pager1.TotalRecords; }
            set { Pager1.TotalRecords = value; }
        }

        #endregion

        #region [Event Handlers]

        protected void spgrdPrintableItems_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if(e.Row.RowType != DataControlRowType.DataRow) return;
                DocumentToPrint item = e.Row.DataItem as DocumentToPrint;
                if (item == null) return;

                //Configuring images
                Image imgIncluded = e.Row.FindControl("imgIncluded") as Image;
                Image imgPrinted = e.Row.FindControl("imgPrinted") as Image;
                Image imgSent = e.Row.FindControl("imgSent") as Image;
                Image imgReturned = e.Row.FindControl("imgReturned") as Image;
                imgIncluded.Visible = item.Incluido.HasValue ? item.Incluido.Value : false;
                imgPrinted.Visible = item.Impreso.HasValue ? item.Impreso.Value : false;
                imgReturned.Visible = item.Devuelto.HasValue ? item.Devuelto.Value : false;
                imgSent.Visible = item.Enviado.HasValue ? item.Enviado.Value : false;

                //Configuring button action
                ImageButton imgButton = e.Row.FindControl("btnPrintElementxx") as ImageButton;
                imgButton.CommandArgument = item.Id+"&"+item.Type+"&"+item.Path;
            }
            catch(Exception ex)
            {
                HandleError(ex);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                _Presenter = new PrintableDocumentListPresenter(this);
                if (Page.IsPostBack) return;
                _Presenter.HandleLoad();
            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            Pager1.OnPageChanged += new Pager.PageChangeHandler(Pager1_OnPageChanged);
            //Pager1.NextLinkText = "Siguiente";
            //Pager1.PreviousLinkText = "Anterior";
            Pager1.NumberOfPagesToDisplay = 10;
            PageSize = 15;
            PageIndex = 0;
        }

        protected void Pager1_OnPageChanged(object sender, PageChangeEventArgs e)
        {
            OnPageChanged(sender, e);
        }

        protected void btnPrintElement_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                ImageButton currentButton = sender as ImageButton;
                if (UpdateRecordStatus)
                {
                    this.SelectedItemId = Convert.ToInt32(currentButton.CommandArgument.Split('&')[0]);
                    this.SelectedContentType = currentButton.CommandArgument.Split('&')[1];
                    _Presenter.MarkUserDocumentAsPrinted(Microsoft.SharePoint.SPContext.Current.Web.CurrentUser.LoginName);
                    this.Notify();
                }
                else
                { 
                    this.SelectedDocumentPath = currentButton.CommandArgument.Split('&')[2];
                    this.ShowDocument();
                }

            }
            catch (Exception ex)
            {
                HandleError(ex);
            }
        }

        #endregion

        #region [Public Members]

        public event PageChangeHandler OnPageChanged;
        public delegate void PageChangeHandler(object sender, EventArgs e);

        #endregion

        #region Implemented Methods

        public void Show() 
        {
            spgrdPrintableItems.DataSource = Documents;
            spgrdPrintableItems.DataBind();
            Pager1.BuildPager();
        }

        public void ShowDocument()
        {
            this.OpenDocument(this.SelectedDocumentPath);
        }

        #endregion

        #region Observable Implementation

        public void Notify()
        {
            try
            {
                ParentSubject.NotifyChanges();                
            }
            catch(Exception ex)
            {
                HandleError(ex);
            }
        }

        public void ExecuteAction()
        {
            try
            {
                _Presenter.HandleLoad();
            }
            catch(Exception ex)
            {
                HandleError(ex);
            }     
        }

        #endregion

    }
}
